package com.ys.otms.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ys.otms.constant.WxConstant;
import com.ys.otms.services.IUserServices;
import com.ys.otms.utils.EncryptionUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserServices iUserServices;

    @ResponseBody
    @RequestMapping(value = "user/queryUser",method = RequestMethod.GET)
    //查询当前登录人信息
    public JSONObject queryUser(HttpServletRequest request) {
        logger.info("UserController queryUser start ");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iUserServices.queryUser(request);
        } catch (Exception e) {
            logger.error("UserController queryUser Exception", e);
            retObj.put("SUCCESS",false);
            retObj.put("MSG","查询错误");
        }
        return retObj;
    }

    @ResponseBody
    @RequestMapping(value = "user/editPassword",method = RequestMethod.POST)
    //修改当前登录人密码
    public JSONObject editPassword(HttpServletRequest request) {
        logger.info("UserController editPassword start ");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iUserServices.editPassword(request);
        } catch (Exception e) {
            logger.error("UserController editPassword Exception", e);
            retObj.put("SUCCESS",false);
            retObj.put("MSG","system error");
        }
        return retObj;
    }

    @ResponseBody
    @RequestMapping("user/queryAllCompanyUser")
    //查询公司下所有账户
    public String queryAllCompanyUser(HttpServletRequest request, @RequestParam(value = "page") String page, @RequestParam(value = "limit") String limit) throws Exception {
        logger.info("UserController queryUser start ");
        Map<String, Object> result = new HashMap<>();
        try {
            result = iUserServices.queryAllCompanyUser(request,page, limit);
        } catch (Exception e) {
            logger.error("UserController queryUser Exception", e);
            result.put("SUCCESS",false);
            result.put("MSG","查询错误");
        }
        result.put("code","");
        result.put("msg","");
        return new ObjectMapper().writeValueAsString(result);
    }


    @ResponseBody
    @RequestMapping(value = "user/addUser",method = RequestMethod.POST)
    //添加账户
    public JSONObject addUser(HttpServletRequest request) {
        logger.info("UserController queryUser start ");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iUserServices.addUser(request);
        } catch (Exception e) {
            logger.error("UserController queryUser Exception", e);
            if (e.getMessage().contains("扣费失败")) {
                retObj.put("SUCCESS",false);
                retObj.put("MSG",e.getMessage());
            }
            else {
                retObj.put("SUCCESS",false);
                retObj.put("MSG","查询错误");
            }

        }
        return retObj;
    }

    @ResponseBody
    @RequestMapping(value = "user/deleteUser",method = RequestMethod.POST)
    //删除账户
    public JSONObject deleteUser(HttpServletRequest request) {
        logger.info("UserController deleteUser start ");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iUserServices.deleteUser(request);
        } catch (Exception e) {
            logger.error("UserController deleteUser Exception", e);
            retObj.put("SUCCESS",false);
            retObj.put("MSG","查询错误");
        }
        return retObj;
    }

    @ResponseBody
    @RequestMapping(value = "user/editUser",method = RequestMethod.POST)
    //修改用户角色
    public JSONObject editUser(HttpServletRequest request) {
        logger.info("UserController editUser start ");
        JSONObject retObj = new JSONObject();
        try {
            retObj = iUserServices.editUser(request);
        } catch (Exception e) {
            logger.error("UserController editUser Exception", e);
            retObj.put("SUCCESS",false);
            retObj.put("MSG","查询错误");
        }
        return retObj;
    }

    @RequestMapping(value = "user/getWxBindUrl",method = RequestMethod.GET)
    @ResponseBody
    public JSONObject getWxBindUrl(HttpServletRequest request) {
        logger.info("LoginController getWxBindUrl start ");
        String userId = (String) request.getSession().getAttribute("SESSION_USER_ID");
        String phoneNum = (String) request.getSession().getAttribute("SESSION_PHONE_NUM");
        String redirectUri = "http%3a%2f%2fwww2.yihangotms.com%2fotms2%2fwxBindCallback.do";
        String state = EncryptionUtil.jdkBase64Encode(userId+"&"+phoneNum);
        request.getSession().setAttribute("LOGIN_STATE", state);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("redirectUri",redirectUri);
        jsonObject.put("state",state);
        jsonObject.put("appId",WxConstant.APP_ID);
        return jsonObject;
    }

    @RequestMapping("/wxBindCallback")
    public ModelAndView wxBindCallback(HttpServletRequest request) {
        logger.info("UserController wxBindCallback start ");
        ModelAndView modelAndView = new ModelAndView();
        try {
            modelAndView = iUserServices.wxBindCallback(request);
        } catch (Exception e) {
            logger.error("UserController wxBindCallback Exception", e);
            modelAndView.addObject("SUCCESS",false);
            modelAndView.addObject("MSG","绑定失败");
        }
        modelAndView.setViewName("index");
        modelAndView.addObject("weChat","Y");
        return modelAndView;
    }

}
